import { _decorator, Component, Label, Node,Sprite,SpriteFrame } from 'cc';
import { Turntable } from './Turntable';
const { ccclass, property } = _decorator;

@ccclass('Press')
export class Press extends Component {

    @property({ type: SpriteFrame })
    iconSpriteFrame1: SpriteFrame = null;
    @property({ type: SpriteFrame })
    iconSpriteFrame2: SpriteFrame = null;

    
    @property(Node)
    popAuto: Node = null; 

    @property(Label)
    autoLab: Label = null; 

    @property(Node)
    startGame: Node = null; 

    private autoIndex=0;
    private autoCounts=[10000000,100,50,20,10];

    private autoNode0:Node=null;
    private autoNode1:Node=null;
    private autoNode2:Node=null;
    private autoNode3:Node=null;
    private autoNode4:Node=null;

    private autoNodeClose:Node=null;


    private touchStartTime: number = 0;
    private  static isAuto: boolean = false;
    private btn: Sprite = null;
    private gameSrc: Turntable;


    onLoad() {
        this.btn=this.startGame.getComponent(Sprite);
        this.startGame.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.startGame.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        let kernel=this.popAuto.getChildByName('Kernel');
        this.autoNode0=kernel.getChildByName('Label-001')
        this.autoNode1=kernel.getChildByName('Label-002')
        this.autoNode2=kernel.getChildByName('Label-003')
        this.autoNode3=kernel.getChildByName('Label-004')
        this.autoNode4=kernel.getChildByName('Label-005')
        this.autoNodeClose=this.popAuto.getChildByName('SpriteBG');

        this.autoNode0.on(Node.EventType.TOUCH_START, this.onClick0, this);
        this.autoNode1.on(Node.EventType.TOUCH_START, this.onClick1, this);
        this.autoNode2.on(Node.EventType.TOUCH_START, this.onClick2, this);
        this.autoNode3.on(Node.EventType.TOUCH_START, this.onClick3, this);
        this.autoNode4.on(Node.EventType.TOUCH_START, this.onClick4, this);
        this.autoNodeClose.on(Node.EventType.TOUCH_START, this.onClickClose, this);

        
    }
    private timerTask;

    private onTouchStart() {
        this.touchStartTime = Date.now();
        this.timerTask=   setTimeout(() => {
           
            this.showPopup();
        }, 1000);

    }
    public add(gameSrc:Turntable ){
        this.gameSrc=gameSrc;
    }
    private onTouchEnd() {
        const touchEndTime = Date.now();
        const duration = touchEndTime - this.touchStartTime;
        //console.log('点击=',duration);
        if (duration < 1000) { // 长按阈值
            this.btn.spriteFrame=this.iconSpriteFrame1;
            this.gameSrc. startGame();
            this.gameSrc. stopAuto();
            clearTimeout( this.timerTask)
           
        }
       
    }

    private showPopup() {
        let isShow=this.popAuto.active;
        if (!isShow&&!this.gameSrc.getAutoFlag()) {
            this.popAuto.active = true; // 显示对话框节点      
        }
       
    }

    private onClick0(){
        this. autoIndex=0; 
        this. initAuto(this. autoIndex);
       
    
    }
    private onClick1(){
        this. autoIndex=1; 
        this. initAuto(this. autoIndex);
    }
    private onClick2(){
        this. autoIndex=2; 
        this. initAuto(this. autoIndex);
        
    }
    private onClick3(){
        this. autoIndex=3; 
        this. initAuto(this. autoIndex);
        
    }
    private onClick4(){
        this. autoIndex=4; 
        this. initAuto(this. autoIndex);
        
    }

    private onClickClose(){
     
        this.popAuto.active=false;
        
    }

    private initAuto(autoIndex:number){
       // console.log('点击=',autoIndex);
        let autoCount=this.autoCounts[autoIndex]
        this.gameSrc.setAutoCounts(autoCount);
        this.popAuto.active=false;
        this.btn.spriteFrame=this.iconSpriteFrame2;
        this.gameSrc. autoGame();
        
      
    }
    protected update(dt: number): void {
        let auto= this.gameSrc.getAutoCounts();
        if (auto==0) {
            this.btn.spriteFrame=this.iconSpriteFrame1; 
            this.autoLab.node.active=false;
        }else{
            this.autoLab.node.active=true;
            
            if (auto>100) {
                this.autoLab.string= "∞";
            }else{
                this.autoLab.string= auto+"";
            }
           
        }
      
    }
 
}


